<?php
namespace App\Http\Controllers\Account;
use App\Http\Controllers\Validator\AdminVail;
use App\Model\Account\Admin;
use App\Tool\Error;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class AdminController extends  Controller
{
    //内部员工列表
    public function adminList(Request $request)
    {
        $params = $request->all();
        $where = [];
        //员工名字
        if ($request->input('a_user_name')){
            $where[] = ['a_user_name', 'like', "%".$params['a_user_name']."%"];
        }
        $list = Admin::adminList(env('PAGE_LIMIT_ADMIN'),$where);
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

    //添加
    public function adminAdd(Request $request)
    {
        $params = $request->all();
        $vali = AdminVail::addAdmin($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Admin::addAdmin($params);
        if ($status === -1){
            return $this->error(Error::ALREADY_REGISTER);
        } else {
            if (!$status) {
                return $this->error(Error::ADD_ERROR);
            }
            return $this->success('ok', '添加成功');
        }
    }


    //修改
    public function adminEdit(Request $request)
    {
        $params = $request->all();
        $vali = AdminVail::editAdmin($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Admin::adminEdit($params);
        if (!$status) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '修改成功');
    }


    //禁用启用
    public function updateStatus(Request $request)
    {
        $params = $request->all();
        $status = Admin::updateStatus($params);
        if (!$status) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '修改成功');
    }

    //修改密码
    public function updatePass(Request $request)
    {
        $params = $request->all();
        $vali = AdminVail::updatePass($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Admin::updatePass($params);
        if ($status === -1){
            return $this->error(Error::TWO_PASSWORD_ERROR);
        } elseif ($status === -2){
            return $this->error(Error::OLD_PASSWORD_ERROR);
        } else {
            if(!$status) {
                return $this->error(Error::EDIT_ERROR);
            } else {
                return $this->success('ok', '修改成功');
            }
        }
    }
    //登录
    public function login(Request $request)
    {
        $params = $request->all();
        $vali = AdminVail::login($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Admin::login($params);
        if ($status === -1){
            return $this->error(Error::USERNAME_NULL);
        }
        if (!$status) {
            return $this->error(Error::PASSWORD_ERROR);
        } else {
            return $this->success('ok', $status);
        }
    }
}